<template>
  <a-row>
    <a-col flex="auto">
      <a-card :bordered="false" style="margin-bottom: 10px">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper" style="position: relative">
          <div class="search-wrapper-btn">
            <a-button type="link" @click="show = !show">
              <span v-if="!show">展开
                <a-icon type="down" />
              </span>
              <span v-else>收起
                <a-icon type="up" />
              </span>
            </a-button>
          </div>
          <a-form-model :label-col="labelCol" :wrapper-col="wrapperCol" @keyup.enter.native="onQuery">
            <div class="mode_item">
              <div class="mode_item_div">
                <a-form-model-item label="数据来源">
                  <a-select v-model="pageFilter.pushSource" allowClear placeholder="请选择" style="width:100%;">
                    <a-select-option v-for="(item, key) in sourceArr" :key="key" :value="item.value">{{ item.title }}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </div>
              <div class="mode_item_div">
                <a-form-model-item label="封堵范围">
                  <a-range-picker v-model="time1" allowClear style="width:100%;" format="YYYY-MM-DD"
                    valueFormat="YYYY-MM-DD" @change="onTimeChange1" />
                </a-form-model-item>
              </div>
              <div class="mode_item_div">
                <a-form-model-item label="接收范围">
                  <a-range-picker v-model="time2" allowClear style="width:100%;" format="YYYY-MM-DD"
                    valueFormat="YYYY-MM-DD" @change="onTimeChange2" />
                </a-form-model-item>
              </div>
            </div>
            <div class="mode_item" v-show="show">
              <div class="mode_item_div">
                <a-form-model-item label="分发类型">
                  <a-select v-model="pageFilter.distributeType" allowClear placeholder="请选择" style="width:100%;">
                    <a-select-option v-for="(item, key) in ffTypeArr" :key="key" :value="item.value">{{
                      item.title
                    }}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </div>
              <div class="mode_item_div">
                <a-form-model-item label="分发状态">
                  <a-select v-model="pageFilter.distributeStatus" allowClear placeholder="请选择" style="width:100%;">
                    <a-select-option v-for="(item, key) in statusArr" :key="key" :value="item.value">{{ item.title }}
                    </a-select-option>
                  </a-select>
                </a-form-model-item>
              </div>
              <div class="mode_item_div">
                <a-form-model-item label="分发时间">
                  <a-range-picker v-model="time3" allowClear style="width:100%;" format="YYYY-MM-DD"
                    valueFormat="YYYY-MM-DD" @change="onTimeChange3" />
                </a-form-model-item>
              </div>
            </div>
            <div class="mode_item" v-show="show">
              <div class="mode_item_div">
                <a-form-model-item label="批次号">
                  <a-input v-model="pageFilter.batchNo" placeholder="请输入" />
                </a-form-model-item>
              </div>
            </div>
          </a-form-model>
          <div class="btnBox">
            <a-button type="primary" @click="onQuery"> 查询 </a-button>
            <a-button style="margin-left: 10px" @click="onResetForm"> 重置 </a-button>
          </div>
        </div>
      </a-card>
      <a-card :bordered="false">
        <!-- 操作按钮区域 -->
        <div class="table-operator">
          <a-button type="primary" @click="onSend(1)">手工分发</a-button>
          <span style="margin-left:20px;font-size:14px;">分发数量：<span style="color:#108EE9ed">{{ distributeNum }}</span>
            条</span>
        </div>
        <!-- 表格展示 -->
        <div class="table-page-content">
          <z-table ref="table" rowKey="id" :showAlert="false" :showSelection="true" :loading="loading"
            :columns="columns" :dataSource="pageData" @change="onTableChange" @selectedRow="onSelectedRow">
            <template slot="distributeType" slot-scope="text">
              <!-- <span>手工分发</span> -->
              <span v-if="text == 0">手工分发</span>
              <span v-if="text == 1">自动分发</span>
            </template>
            <template slot="distributeStatus" slot-scope="text">
              <span v-if="text == 0">未分发</span>
              <span v-if="text == 1">分发成功</span>
              <span v-if="text == 2">分发失败</span>
            </template>
            <template slot="action" slot-scope="text,record">
              <template v-if="record.distributeStatus != 1">
                <a style="color:#108EE9" @click="onSend(2, record)">分发</a>
                <a-divider type="vertical" />
              </template>
              <a style="color:#108EE9" @click="onDetail(record)">详情</a>
            </template>
          </z-table>
        </div>
      </a-card>
    </a-col>
  </a-row>
</template>

<script>
import moment from 'moment'
import { fd_db_manualDistributeList, fd_db_distributeBatch } from '@/api/plugging/authority/fd.js'
export default {
  data() {
    return {
      labelCol: { span: 6 },
      wrapperCol: { span: 17 },
      show: false,
      loading: false,
      visible: false,
      reloading: false,
      time1: [],
      time2: [],
      time3: [],
      sourceArr: [
        { title: "全部", value: 0, },
        { title: "人工推送任务", value: 1, },
        { title: "自动推送任务", value: 2, },
      ],
      ffTypeArr: [
        { title: "全部", value: '', },
        { title: "手工分发", value: 0, },
        { title: "自动分发", value: 1, },
      ],
      statusArr: [
        { title: "全部", value: '', },
        { title: "未分发", value: 0, },
        { title: "分发成功", value: 1, },
        { title: "分发失败", value: 2, }
      ],
      columns: [
        {
          title: '序号',
          dataIndex: 'order',
          width: 60,
          align: 'center',
          customRender: (text, record, index) =>
            `${(this.pageFilter.pageNo - 1) * this.pageFilter.pageSize + (index + 1)}` //序号分页后连续
        },
        {
          title: '批次号',
          dataIndex: 'batchNo',
          width: 160,
          align: 'center',
          customRender: (text, record) => {
            return (
              <j-ellipsis value={text} length={12} />
            )
          }
        },
        {
          title: '管局接收时间',
          dataIndex: 'receiveTime',
          width: 90,
          align: 'center',
        },
        {
          title: '分发类型',
          align: 'center',
          width: 80,
          dataIndex: 'distributeType',
          scopedSlots: { customRender: 'distributeType' },
        },
        {
          title: '分发状态',
          dataIndex: 'distributeStatus',
          width: 100,
          align: 'center',
          scopedSlots: { customRender: 'distributeStatus' },
        },
        {
          title: '分发时间',
          dataIndex: 'distributeTime',
          width: 100,
          align: 'center',
        },
        {
          title: '操作',
          width: 80,
          align: 'center',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
        },
      ],
      pageFilter: {
        pageNo: 1,
        pageSize: 10,
      },
      pageData: {},
      batchNoList: [],
      rangTime: [],
      distributeNum: 0
    }
  },
  created() {
    this.onQuery()
  },

  methods: {
    onQuery() {
      this.pageFilter.pageNo = 1
      this.refreshTable()
    },
    onResetForm() {
      this.pageFilter = {
        pageNo: 1,
        pageSize: 10,
      }
      this.time1 = []
      this.time2 = []
      this.time3 = []
      this.refreshTable()
    },
    onTableChange(pagination, pageFilter, sorter) {
      let keyMap = { currentPage: 'pageNo' }
      let paginations = Object.keys(pagination).reduce((newData, key) => {
        let newKey = keyMap[key] || key
        newData[newKey] = pagination[key]
        return newData
      }, {})
      this.pageFilter = Object.assign({}, this.pageFilter, paginations)
      this.refreshTable()
    },
    refreshTable() {
      this.loading = true
      fd_db_manualDistributeList(this.pageFilter).then((res) => {
        if (res.success) {
          this.pageData = res.result ? res.result : {}
          this.loading = false
        } else {
          this.pageData = {}
          this.$message.warn(res.message)
          this.loading = false
        }
      })
        .catch((e) => {
          this.loading = false
          this.$message.warn('请求失败！')
        })
    },
    onTimeChange1(date) {
      this.$set(this.pageFilter, 'startDiscoveryTime', date[0])
      this.$set(this.pageFilter, 'endDiscoveryTime', date[1])
    },
    onTimeChange2(date) {
      this.$set(this.pageFilter, 'startReceiveTime', date[0])
      this.$set(this.pageFilter, 'endReceiveTime', date[1])
    },
    onTimeChange3(date) {
      this.$set(this.pageFilter, 'startDistributeTime', date[0])
      this.$set(this.pageFilter, 'endDistributeTime', date[1])
    },
    setTittle(text) {
      return this.$filterDictTextByCache('personType', text)  //现在字典中对应的文字，是全局函数
    },
    onSelectedRow(row) {
      // console.log(row)
      this.batchNoList = []
      this.rangTime = []
      this.distributeNum = 0
      let discoveryTime = []
      if (row.length > 0) {
        row.forEach(item => {
          this.batchNoList.push(item.batchNo)
          this.distributeNum += item.distributeNum
          // console.log(item.discoveryTime)
          discoveryTime.push(new Date(item.discoveryTime))
        });
      }
      var maxDate = new Date(Math.max.apply(null, discoveryTime)); //最大日期
      var minDate = new Date(Math.min.apply(null, discoveryTime)); //最小日期
      this.rangTime = [moment(minDate).format('YYYY-MM-DD'), moment(maxDate).format('YYYY-MM-DD')]
      // console.log(this.rangTime)
    },
    onSend(type, record) {
      if (type == 1) { //手工分发
        if (this.batchNoList.length <= 0) {
          this.$message.warn("请勾选列表数据！")
          return
        }
        if (this.distributeNum > 300000) {
          this.$message.warning('分发数量不能超过三十万条！')
          return
        }
        this.$router.push({
          path: "/authority/fd_distribution/add",
          query: {
            flag: 1,
            batchNoList: this.batchNoList,
            rangTime: this.rangTime
          }
        })
      } else { //列表数据直接分发
        let that = this;
        this.$confirm({
          title: `你确定要分发该数据吗？`,
          okText: "确认",
          okType: "danger",
          cancelText: "取消",
          onOk() {
            // let api = ''
            // if (that.policyStatus == 'fd_db') api = fdApi.fd_db_deleteBatch({ ids: that.idArr.join() })
            // if (that.policyStatus == 'fd_fb') api = fdApi.fd_fb_deleteBatch({ ids: that.idArr.join() })
            // if (that.policyStatus == 'jf_db') api = jfApi.jf_db_deleteBatch(that.idArr)
            // if (that.policyStatus == 'jf_fb') api = jfApi.jf_fb_deleteBatch(that.idArr)
            fd_db_distributeBatch({ batchNoList: [record.batchNo] }).then((res) => {
              that.$message.success(res.message)
              that.onQuery();
            });
          }
        });
      }
    },
    //查看详情
    onDetail(record) {
      this.$router.push({
        path: "/authority/fd_distribution/detail",
        query: {
          flag: 2,
          batchNoList: [record.batchNo],
          rangTime: [record.discoveryTime, record.discoveryTime]
        }
      })
    },
    /** 重新加载组件 */
    reload() {
      this.reloading = true
      this.$nextTick(() => (this.reloading = false))
    },
  },
}
</script>
<style lang="less" scoped>
.mode_item {
  display: flex;
  border-bottom: 1px #dadada dashed;

  .mode_item_div {
    width: 30%;
    padding: 5px 0;

    .ant-form-item {
      margin-bottom: 0;
    }
  }

  &>* {
    width: 100%;
    margin-bottom: 0;
    padding: 10px 0;
  }
}

.search-wrapper-btn {
  position: absolute;
  right: 0%;
  top: 9px;
  z-index: 100;
}

.btnBox {
  margin-top: 10px;
  // position: absolute;
  // right: 0;
  // top: 5px;
  // z-index: 120;
}

@import '~@assets/less/common.less';
</style>
